package com.lujianfei.powerinput.ui.noteList.presenter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.lujianfei.powerinput.database.NoteItemFieldModel;
import com.lujianfei.powerinput.database.NoteItemModel;
import com.lujianfei.powerinput.ui.noteList.base.NoteListEditContract;
import com.lujianfei.powerinput.ui.noteList.base.NoteListViewContract;
import com.lujianfei.powerinput.utils.ConfigUtils;
import com.lujianfei.powerinput.widget.input.base.IWidgetInput;
import com.lujianfei.powerinput.widget.input.factory.InputFactory;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class NoteListViewPresenter implements NoteListViewContract.Presenter {
    private final static String TAG = "NoteListAddPresenter";
    private final NoteListViewContract.View view;
    private NoteItemModel data;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    public NoteListViewPresenter(NoteListViewContract.View view) {
        this.view = view;
    }

    @Override
    public void init() {
        if (null!=data) {
            Observable<List<NoteItemFieldModel>> observable = Observable.create(new ObservableOnSubscribe<List<NoteItemFieldModel>>() {
                @Override
                public void subscribe(ObservableEmitter<List<NoteItemFieldModel>> emitter) throws Exception {
                    List<NoteItemFieldModel> noteItemFieldModels = NoteItemFieldModel.find(NoteItemFieldModel.class, "noteItemId=?", "" + data.getId());
                    emitter.onNext(noteItemFieldModels);
                }
            });
            DisposableObserver<List<NoteItemFieldModel>> disposableObserver = new DisposableObserver<List<NoteItemFieldModel>>() {
                @Override
                public void onNext(List<NoteItemFieldModel> noteItemFieldModels) {
                    view.updateList(noteItemFieldModels);
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            };
            observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(disposableObserver);

        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent dataIntent) {
        if(resultCode!=Activity.RESULT_OK){
            return;
        }
    }

    @Override
    public void setData(NoteItemModel data) {
        this.data = data;
    }

    @Override
    public void onDestroy() {
        compositeDisposable.clear();
    }
}
